package tjo;

import static org.junit.Assert.assertTrue;
import robocode.Rules;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * This tests for variability in bullet power administered by the robot Meteor.
 * 
 * Modified from original code by Philip Johnson.
 * 
 * @author Jordan Takayama
 * @since October 6, 2011
 */
public class TestMeteorFiring extends RobotTestBed {
  // True if Meteor has fired a bullet with power less than or equal to 0.9;
  boolean firePowerReachLevelOne = false;
  // True if Meteor has fired a bullet with power between 0.9 to 1.9;
  boolean firePowerReachLevelTwo = false;
  // True if Meteor has fired a bullet with power between 2.0 to 2.9;
  boolean firePowerReachLevelThree = false;
  // True if Meteor has fired a bullet with power equal to Rules.MAX_BULLET_POWER (which is 3.0);
  boolean firePowerReachLevelFour = false;

  /**
   * Specifies that SittingDuck and Meteor are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    // TO DO: Check that this actually results in varying levels of bullet power...
    return "sample.SittingDuck,tjo.Meteor";
  }

  /**
   * This test runs for 20 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 20;
  }

  /**
   * At the end of each turn, checks the power of all bullets moving across the battlefield. Checks
   * to see if there is any variability in bullet power, proving that Meteor is using proportional
   * firing based on distance of detected enemy robot.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    // All active bullets belong to Meteor since SittingDuck does not fire.
    IBulletSnapshot bullets[] = event.getTurnSnapshot().getBullets();
    double bulletPower;

    for (int i = 0; i < bullets.length; i++) {

      bulletPower = bullets[i].getPower();

      if (bulletPower <= 0.9) {
        firePowerReachLevelOne = true;
      }
      else if (bulletPower > 0.9 && bulletPower <= 1.9) {
        firePowerReachLevelTwo = true;
      }
      else if (bulletPower >= 2.0 && bulletPower <= 2.9) {
        firePowerReachLevelThree = true;
      }
      else if (bulletPower == Rules.MAX_BULLET_POWER) {
        firePowerReachLevelFour = true;
      }
    }
  }

  /**
   * After running all matches, determine if Meteor has had variability in its bullet power.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Bullet Power less than 0.9", firePowerReachLevelOne);
    assertTrue("Bullet Power between 0.9 and 1.9", firePowerReachLevelTwo);
    assertTrue("Bullet Power between 2.0 and 2.9", firePowerReachLevelThree);
    assertTrue("Bullet Power equal to 3.0", firePowerReachLevelFour);
  }
}